﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using M7;

namespace S7.Windows.Settings
{
    /// <summary>
    /// Interaction logic for EnableDisableModules.xaml
    /// </summary>
    public partial class EnableDisableModules : Window
    {
        public EnableDisableModules()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (!Properties.Settings.Default.ChatModule)
            {
                cbChat.IsChecked = false;
            }
            if (!Properties.Settings.Default.CheckOutModule)
            {
                cbCheckout.IsChecked = false;
            }
            if (!Properties.Settings.Default.GamesModule)
            {
                cbGames.IsChecked = false;
            }
            if (!Properties.Settings.Default.InternetModule)
            {
                cbInternet.IsChecked = false;
            }
            if (!Properties.Settings.Default.MemoireModule)
            {
                cbMemoire.IsChecked = false;
            }
            if (!Properties.Settings.Default.MenuModule)
            {
                cbMenu.IsChecked = false;
            }
            if (!Properties.Settings.Default.PersonalizationModule)
            {
                cbPersonalization.IsChecked = false;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.ChatModule = (bool)cbChat.IsChecked;
            Properties.Settings.Default.CheckOutModule = (bool)cbCheckout.IsChecked;
            Properties.Settings.Default.GamesModule = (bool)cbGames.IsChecked;
            Properties.Settings.Default.InternetModule = (bool)cbInternet.IsChecked;
            Properties.Settings.Default.MemoireModule = (bool)cbMemoire.IsChecked;
            Properties.Settings.Default.MenuModule = (bool)cbMenu.IsChecked;
            Properties.Settings.Default.PersonalizationModule = (bool)cbPersonalization.IsChecked;
            Properties.Settings.Default.Save();
            Thread thr = new Thread(new ThreadStart(() =>
                {
                    try
                    {
                        Dictionary<enumModules, bool> moduleOptions = AdminTools.GetModules();
                        Info.window.Dispatcher.Invoke(new ThreadStart(() =>
                            {
                        foreach (var item in Info.window.wrpLiveView.Children.OfType<TableControl>())
                        {
                            if (item.Connection != null)
                            {
                                Msg SendMsg = new Msg();
                                SendMsg.enumMethod = enumAdminTools.DisableModule;
                                SendMsg.obj = moduleOptions;
                                SendMsg.IP = item.Connection.current.IP;
                                SendMsg.port = item.Connection.current.Port;
                                Info.server.SendingQueue.Enqueue(SendMsg);
                            }
                        }
                    }));
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                    }
                }));
            thr.IsBackground = true;
            thr.Start();
            this.Close();
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
